﻿namespace ExamV5.StudyInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmEditMoreLeesionInfo : Page
    {
        protected Button butClose;
        protected Button butDefault;
        protected DropDownList DDLType;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private PublicFun publicFun_0 = new PublicFun();
        protected TextBox txtFen;
        protected TextBox txtMainUser;
        protected TextBox txtMemberFen;
        protected TextBox txtUnitName;
        protected HtmlInputFile UPUserData;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            string str = "";
            string fileName = this.UPUserData.PostedFile.FileName;
            if (!(fileName.Trim() != ""))
            {
                base.Response.Write("<script>alert('文件格式不正确！请选择扩展名为xls或xlsx的文件！')</script>");
            }
            else
            {
                try
                {
                    string str9 = fileName.Substring(fileName.LastIndexOf('.'));
                    string str10 = ".XLS.XLSX";
                    if (str10.IndexOf(str9.ToUpper()) >= 0)
                    {
                        if (fileName.IndexOf(@"\", 0) > 0)
                        {
                            int num = fileName.LastIndexOf(@"\");
                            fileName = fileName.Substring(num + 1);
                            fileName = this.httpCookie_0.Value + "xls" + str9;
                        }
                        this.UPUserData.PostedFile.SaveAs(base.Server.MapPath("../") + @"\PublicFile\" + fileName);
                        string str8 = base.Server.MapPath("../") + @"\PublicFile\" + fileName;
                        string connectionString = "";
                        if (fileName.ToUpper().IndexOf(".XLSX") == -1)
                        {
                            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str8 + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                        }
                        else
                        {
                            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str8 + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
                        }
                        OleDbConnection connection = new OleDbConnection(connectionString);
                        connection.Open();
                        string str3 = "";
                        string str4 = "";
                        SqlConnection connection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection2.Open();
                        SqlTransaction transaction = connection2.BeginTransaction();
                        SqlCommand command2 = new SqlCommand {
                            Connection = connection2,
                            CommandTimeout = 0,
                            Transaction = transaction
                        };
                        try
                        {
                            OleDbCommand command = new OleDbCommand("select * from [sheet1$]", connection);
                            OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                            while (reader.Read())
                            {
                                if ((reader[0].ToString() != "") && (reader[1].ToString() != ""))
                                {
                                    str3 = reader[1].ToString().Trim();
                                    str4 = reader[0].ToString().Trim();
                                    string str5 = "LessionHome.jpg";
                                    str = "insert into LessionInfo(LessionLoadFile,LessionName,LessionTypeID,InputUnit,MainCheckUser,SortInfo,CreatUser,FenValue,RemoveFen,LessionImage) values(";
                                    str = str + "'" + str3 + "','" + str4 + "'," + this.DDLType.SelectedItem.Value + ",'" + this.txtUnitName.Text.Trim() + "','" + this.txtMainUser.Text.Trim() + "','1','" + this.httpCookie_0.Value + "',";
                                    str = str + "'" + this.txtFen.Text.Trim() + "','" + this.txtMemberFen.Text.Trim() + "','" + str5 + "')";
                                    command2.CommandText = str;
                                    command2.ExecuteNonQuery();
                                }
                            }
                            reader.Close();
                            command.Dispose();
                            connection.Close();
                            connection.Dispose();
                            transaction.Commit();
                            base.Response.Write("<script>alert('批量导入课件信息操作成功！');parent.location.reload();WindowClosed();</script>");
                        }
                        catch
                        {
                            transaction.Rollback();
                            base.Response.Write("<script>alert('批量导入课件信息操作出错！请检查文件格式是否正确！" + str4 + "')</script>");
                        }
                        finally
                        {
                            connection2.Close();
                            connection2.Dispose();
                            connection.Close();
                            connection.Dispose();
                        }
                    }
                    else
                    {
                        base.Response.Write("<script>alert('文件格式不正确！请选择扩展名为xls或xlsx的文件')</script>");
                    }
                }
                catch
                {
                    base.Response.Write(@"<script>alert('导入数据失败！\n\n请将PublicFile目录设置为Everyone用户可修改权限！')</script>");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) && (this.httpCookie_0.Value != ""))
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                base.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString + "---批量导入电子课件";
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) == 0x3a)
                {
                    base.Response.Write("<script>alert('该版本为特惠版，此功能未开放！')</script>");
                }
                else if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                    }
                    else
                    {
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddLession,ManagLession from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddLession"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagLession"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        if (!((this.ViewState["AddPower"].ToString() != "Y") ? !(this.ViewState["ManagPower"].ToString() != "Y") : true))
                        {
                            base.Response.Write("<script>alert('添加或管理电子课件权限未开放！')</script>");
                            this.butDefault.Visible = false;
                        }
                        else
                        {
                            if (!(this.ViewState["ManagPower"].ToString() == "Y"))
                            {
                                this.publicFun_0.GetType(this.DDLType, "LessionType", this.httpCookie_0.Value, "N", this.httpCookie_3.Value);
                            }
                            else
                            {
                                this.publicFun_0.GetType(this.DDLType, "LessionType", this.httpCookie_0.Value, "Y", this.httpCookie_3.Value);
                            }
                            ListItem item = new ListItem("--请选择课件类别--", "0");
                            this.DDLType.Items.Insert(0, item);
                            this.DDLType.SelectedIndex = -1;
                            this.DDLType.Items.FindByValue("0").Selected = true;
                        }
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

